package com.siblings.mvp.presenter;

import android.content.Context;
import com.siblings.mvp.view.MvpView;

import java.lang.ref.WeakReference;

/**
 * SiblingsLoan
 *
 * @Author wwdeng
 * @CreateTime: 2024/7/4 10:07
 * @Annotation：
 */
public class MvpPresenter<V extends MvpView> implements MvpBasePresenter<V> {

    private WeakReference<V> weakView;
    private WeakReference<Context> weakContext;

    public MvpPresenter(Context context){
        this.weakContext = new WeakReference<>(context);
    }

    public Context getContext() {
        return weakContext.get();
    }


    @Override
    public void attachView(V view) {
        this.weakView = new WeakReference<V>(view);
    }


    /**
     * 获取view对象
     * @return 返回当前MvpView
     */
    public V getMvpView(){
        if (weakView == null) {
            return null;
        }

        return weakView.get();
    }


    @Override
    public void detachView() {
        if (this.weakView != null) {
            this.weakView.clear();
            this.weakView = null;
        }
    }
}
